package face.pay.wxpay.controller;


import face.pay.alipay.service.IAliPayService;
import face.pay.dao.EnterpriseInfoDao;
import face.pay.dao.MchPayCodeDao;
import face.pay.dao.TerminalInfoDao;
import face.pay.entity.EnterpriseInfo;
import face.pay.entity.MchPayCode;
import face.pay.entity.TerminalInfo;
import face.pay.util.ResultMessage;
import face.pay.wxpay.service.IPayAuthService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@Slf4j
@RequestMapping("code")
public class PayAuthController {


    @Autowired
    private IPayAuthService iPayAuthService;


    @Autowired
    private IAliPayService iAliPayService;


    @Autowired
    private TerminalInfoDao terminalInfoDao;

    @Autowired
    private EnterpriseInfoDao enterpriseInfoDao;


    @Autowired
    private MchPayCodeDao mchPayCodeDao;

    /**
     * 扫码入口
     *
     * @return
     */
    @RequestMapping(value = "/pay")
    public String payAuth(String code, HttpServletRequest request, Model model) {

        MchPayCode recrod = new MchPayCode();
        recrod.setCode(code);
        MchPayCode mchPayCode = this.mchPayCodeDao.queryMchPayCode(recrod);

        String deviceId = mchPayCode.getDeviceId();

        if (StringUtils.isBlank(deviceId)) {
            model.addAttribute("mchPayCode", mchPayCode);
            return "register";
        }

        TerminalInfo terminalInfo = this.terminalInfoDao.getByDeviceId(deviceId);

        EnterpriseInfo enterpriseInfo = enterpriseInfoDao.queryByUserId(terminalInfo.getUserId());

        String userAgent = request.getHeader("user-agent");
        if (userAgent != null && userAgent.contains("AlipayClient")) {
            log.info("支付宝支付>>>>>>");

            if (StringUtils.isBlank(enterpriseInfo.getAliToken()) || StringUtils.isBlank(enterpriseInfo.getAliUserId())) {
                model.addAttribute("msg", "商家暂没有开通支付宝支付功能,请尝试用微信扫码!!");
                return "other";
            }

            iAliPayService.getAliAuth(deviceId, model);
            return "pay";
        } else if (userAgent != null && userAgent.contains("MicroMessenger")) {
            log.info("微信支付>>>>>>>");

            if (StringUtils.isBlank(enterpriseInfo.getWxSubMchId())) {
                model.addAttribute("msg", "商家暂没有开通微信支付功能,请尝试用支付宝扫码!!");
                return "other";
            }

            String url = iPayAuthService.getPayAuthUrl(deviceId);
            return "redirect:" + url;
        } else {
            model.addAttribute("msg", "请用支付宝或微信扫描二维码!!!!!");
            return "other";
        }

    }

    /**
     * 微信授权回调
     *
     * @return
     */
    @RequestMapping(value = "/WxCallback")
    public String callback(String code, String state, Model model) {
        iPayAuthService.wxCallback(code, state, model);
        return "pay";
    }


    /**
     * 二维码绑定设备
     *
     * @return
     */
    @RequestMapping(value = "/register")
    @ResponseBody
    public ResultMessage register(MchPayCode mchPayCode) {
        return iPayAuthService.register(mchPayCode);

    }
}
